package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @Description
 * @ClassName CartController
 * @Author RC
 * @date 2020.12.21 16:12
 */
@Controller
@RequestMapping("cart")
public class CartController {
    @Reference(timeout = 100000,check = false)
    private CartService cartService;
    private static final Logger logger = (Logger) LoggerFactory.getLogger(CartController.class);

    //查询购物车
    @RequestMapping("cartList")
    @ResponseBody
    public List<Cart> cartList(HttpServletRequest request,HttpServletResponse response){
        //获取用户主体 判断是否登录
        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        //从cookie中查询,cookie中保存的所有的数据都是字符串,如果是对象也是json字符串
        String cartList = CookieUtils.getCookieValue(request, "cartList", "utf-8");
        //如果用户第一次操作cookie是空的,或者是""字符串
        if(StrUtil.hasBlank(cartList) || cartList.equals("")){
            cartList = "[]";
        }
        //使用fastJson将字符串carList转成list
        List<Cart> cartListCookie = JSONObject.parseArray(cartList, Cart.class);
        if(user == null){//未登录查询cookie
            return cartListCookie;
        }else {//如果登录了将cookie和redis合并
            List<Cart> cartListFormRedis = cartService.findCartListFormRedis(user.getUsername());
            //如果cartListCookie有数据在合并
            if(cartListCookie.size() > 0){
                //合并cookie
                List<Cart> margeCarList = cartService.margeCarListCookieAndRedis(cartListCookie,cartListFormRedis);
                //将合并好的购物车,保存在redis
                cartService.saveCartListToRedis(user.getUsername(),margeCarList);
                //合并购物车之后删除cookie
                CookieUtils.deleteCookie(request,response,"cartList");
                return margeCarList;//合并后数据
            }
            return cartListFormRedis;
        }
    }
    @RequestMapping("addCart")
    public String addCart(HttpServletRequest request,HttpServletResponse response,Long itemId,Integer num){
        //添加之前先查询购物车
        List<Cart> cartList = cartList(request,response);

        //调用carService拼接购物车
        cartList = cartService.addCart(cartList,itemId,num);
        //获取用户主体 判断是否登录
         Subject subject = SecurityUtils.getSubject();
         TbUser user =(TbUser) subject.getPrincipal();
         if(user == null){
             CookieUtils.setCookie(request,response,"cartList",JSONObject.toJSONString(cartList),24*60*60,"utf-8");
         }else {
             cartService.saveCartListToRedis(user.getUsername(), cartList);
         }

        return "success-cart";
    }

}
